//@ts-nocheck
//单例模式 --ts写法
class Instens {
  private static _instance: Instens | null
  public name: string
  //使用private后，外部无法直接创建实例
  private constructor(name: string) {
    this.name = name
  }
  static getInstance(name: string): Instens {
    if (Instens._instance === null) {
      Instens._instance = new Instens(name)
    }
    return Instens._instance
  }
}

const i1 = Instens.getInstance('test')

//单例模式 --js写法
/**
 * 由于js没有私有属性，所以使用闭包来模拟私有属性
 */
function genGetInstance() {
  let instance = null
  class Instens {
    name: string
    constructor(name: string) {
      this.name = name
    }
  }
  return (name) => {
    if (instance === null) {
      instance = new Instens(name)
    }
    return instance
  }
}
const Instance = genGetInstance()
const i2 = Instance('test')
